#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

class Solution {
public:
    vector<string> topKFrequent(vector<string> &words, int k) {
        unordered_map<string, int> hashTable;
        int size = words.size();
        for (int i = 0; i < size; ++i) {
            string &word = words[i];
            if (hashTable.find(word) != hashTable.end()) {
                ++hashTable[word];
            } else {
                hashTable[word] = 1;
            }
        }
        vector<pair<string, int>> freq;
        for (auto &word:hashTable) {
            freq.emplace_back(make_pair(word.first, word.second));
        }
        sort(freq.begin(), freq.end(), [](const pair<string, int> &a, const pair<string, int> &b) {
            return a.second > b.second || (a.second == b.second && a.first < b.first);
        });
        vector<string> ans;
        for (int i = 0; i < k; ++i) {
            ans.push_back(freq[i].first);
        }
        return ans;
    }
};

int main() {
    Solution solution;
    vector<string> words = {"i", "love", "leetcode", "i", "love", "coding"};
    solution.topKFrequent(words, 2);

}

